/**
 * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

/**
 * @fileOverview Increase and Decrease Indent commands.
 */

(function() {
    'use strict';

    var TRISTATE_DISABLED = CKEDITOR.TRISTATE_DISABLED,
        TRISTATE_OFF = CKEDITOR.TRISTATE_OFF;

    CKEDITOR.plugins.add('textindent', {
        lang: 'en,pt-br,zh-cn',
        // jscs:enable maximumLineLength
        icons: 'textindent', // %REMOVE_LINE_CORE%
        hidpi: true, // %REMOVE_LINE_CORE%

        init: function(editor) {
            var indentation = editor.config.indentation;
            var indentationKey = editor.config.indentationKey;

            if(typeof(indentation) == 'undefined')
                indentation = '2em';
            if(typeof(indentationKey) == 'undefined')
                indentationKey = 'tab';

            if( indentationKey !== false){
                editor.on('key', function(ev) {
                    if(ev.data.domEvent.$.key.toLowerCase() === indentationKey.toLowerCase().trim() || ev.data.keyCode === indentationKey){
                        editor.execCommand('textindent');
                        ev.cancel();  
                    }
                });
            }

            // Register generic commands.
            editor.addCommand('textindent', {
                allowedContent: 'p{text-indent}',
                requiredContent: 'p',
                exec: function(evt) {
                    var style_textindente = new CKEDITOR.style({
                        element: 'p',
                        styles: { 'text-indent': indentation},
                        overrides: [{
                            element: 'text-indent', attributes: { 'size': '0'}
                        }]
                    });

                    var style_no_textindente = new CKEDITOR.style({
                        element: 'p',
                        styles: { 'text-indent': '0' },
                        overrides: [{
                            element: 'text-indent', attributes: { 'size': indentation}
                        }]
                    });

                    if( style_textindente.checkActive(editor.elementPath(), editor) ){
                        editor.fire('saveSnapshot');
                        editor.applyStyle(style_no_textindente);
                        editor.getCommand('textindent').setState(CKEDITOR.TRISTATE_OFF);
                        editor.getCommand('textindentall').setState(CKEDITOR.TRISTATE_OFF);
                    }
                    else{
                        editor.fire('saveSnapshot');
                        editor.applyStyle(style_textindente);
                        editor.getCommand('textindent').setState(CKEDITOR.TRISTATE_ON);
                    }
                }
            }); 

            // Create and register toolbar button if possible.
            if (editor.ui.addButton) {
              editor.ui.addButton('Textindent', {
                label: editor.lang.textindent.labelName,
                command: 'textindent',
                directional: true,
                toolbar: 'textindent,20',
              });
            }
        }
    });
})();